package com.controller;

import com.domain.Account;
import com.domain.User;
import com.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
    //用来存当前用户名

String [] userString= new String[1];


    //注册界面
    @RequestMapping("/signUp")
    public String signUp(String username,String password,Model model){
        accountService.signUp(username, password);
        model.addAttribute("mess", "注册成功");
//        使用了forward错误信息才会正常显示，使用了redirect会跑到地址栏去
        return "forward:/index.jsp";
    }

    //登录
    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        //每次都重写，保证只存在当前用户
      userString[0]=username;

        accountService.checkUser(username, password);
        model.addAttribute("admin", userString[0]);
        //查出所有user用户
        List<User> users = accountService.findUsers();
        for (User user : users) {
            if(user.getPassword().equals(password)&&user.getUsername().equals(username)){
                return "forward:/account/findAll";
            }
        }

        model.addAttribute("mess", "账号密码密码错误或者用户不存在");
        return "forward:/index.jsp";

    }




    //user表查询所有登录用户
    @RequestMapping("/findUsers")
  public String findUsers(Model model){
        model.addAttribute("admin", userString[0]);


        List<User> userList = accountService.findUsers();
         model.addAttribute("userList", userList);
        return "userList";
  }


    @Autowired
    private AccountService accountService;




    //查询单个
    @RequestMapping("/findByIds")
    public String findByIds(Model model,String name){
        model.addAttribute("admin", userString[0]);
        List<Account> accountList = accountService.findByIds(name);

        if (accountList.isEmpty()){
            model.addAttribute("mess", "没有查到");
            accountService.findAll();

            return "forward:/account/findAll";
        }
        model.addAttribute("mess", "查找成功");
        model.addAttribute("accountList", accountList);
        return "accountList";
    }



    //保存
    @RequestMapping(value = "/save",produces = "text/html;charset=UTF-8")
    public String save(Account account,Model model){

        model.addAttribute("admin", userString[0]);
        model.addAttribute("mess", "添加成功");
        accountService.save(account);
        //添加完跳转到全部界面中去
      return  "forward:/account/findAll";

    }

    //查询出所有
    @RequestMapping("/findAll")
    public String findAll( Model model){
        List<Account> accountList =  accountService.findAll();
        model.addAttribute("admin", userString[0]);
       model.addAttribute("accountList", accountList);

        return "accountList";
    }
//    public ModelAndView findAll(){
//        List<Account> accountList =  accountService.findAll();
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("accountList",accountList);
//        modelAndView.setViewName("accountList");
//        return modelAndView;
//    }


    //查询单个
@RequestMapping("/findById")
    public String findById(Model model,int id){
    model.addAttribute("admin", userString[0]);
        Account accountList = accountService.findById(id);
        model.addAttribute("accountList", accountList);
        return "forward:/account/findOne";
    }

    //删除账户
    @RequestMapping("/deleteById")
    public String deleteById(int id,Model model){
        model.addAttribute("admin", userString[0]);
        accountService.deleteById(id);
        model.addAttribute("mess", "删除成功");
        return  "forward:/account/findAll";
    }

    //修改账户页面

    @RequestMapping("/toUpdateById")
    public String toUpdateById(Model model,int id){
        model.addAttribute("admin", userString[0]);
        Account account = accountService.findById(id);
        model.addAttribute("accounts", account);
        return "updateAccount";
    }

//修改后返回到页面
    @RequestMapping("/updateAccount")
    public String updateAccount(Model model,Account account){
        model.addAttribute("admin", userString[0]);
      accountService.updateAccount(account);
        Account accounts = accountService.findById(account.getId());
        model.addAttribute("accounts", accounts);
            model.addAttribute("mess", "修改成功");
        return "forward:/account/findAll";
    }

//删除user表中用户
    @RequestMapping("/deleteUserById")

    public String deleteUserById(Model model,int id){


        model.addAttribute("admin", userString[0]);
        accountService.deleteUserById(id);
        return "forward:/account/findUsers";
    }



}
